package com.kerrykidz.system.util.sync;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;

import com.rdp.util.config.CusotmizedPropertyUtil;
import com.rdp.util.config.SpringUtil;

/**
 * 
 * @Desc
 * @author QUINN
 * @date 2014年9月17日下午6:41:25
 */
@SuppressWarnings("static-access")
public class SyncParamHelper {

	public static String BROKER_URL;

	public static String BROKER_USER;

	public static String BROKER_PWD;

	public static Date TASK_CYCLE_EVERYDAY_START_DATE;

	public static Date TASK_CYCLE_CONSTANTLY_START_DATE;

	public static String CURRENT_STORE;

	public static String[] ALL_STORE;

	public static int TASK_CYCLE_CONSTANTLY_CYCLE;

	public static String ZONGBU;

	public static boolean LISTEN_MQ_SERVER;

	public static String ZONGBU_FILE_SYNC_QUEUE;

	public static String[] ALL_STORE_FILE_SYNC_QUEUE;

	public static String CURRENT_FILE_QUEUE;

	public static boolean EXECUTE_SYNC_TASK;

	public static void initParam() {
		CusotmizedPropertyUtil pro = SpringUtil.getBeanByType(CusotmizedPropertyUtil.class);
		BROKER_URL = (String) pro.getContextProperty("mq_broker");
		BROKER_USER = (String) pro.getContextProperty("mq_username");
		BROKER_PWD = (String) pro.getContextProperty("mq_pwd");
		CURRENT_STORE = (String) pro.getContextProperty("current_store");
		ZONGBU = (String) pro.getContextProperty("zongbu");
		String allStore = (String) pro.getContextProperty("all_store");
		String listen_mq_server = (String) pro.getContextProperty("listen_mq_server");
		LISTEN_MQ_SERVER = Boolean.parseBoolean(listen_mq_server);

		String task_cycle_constantly_cycle = (String) pro.getContextProperty("task_cycle_constantly_cycle");
		if (StringUtils.isNotBlank(task_cycle_constantly_cycle) && NumberUtils.isNumber(task_cycle_constantly_cycle))
			TASK_CYCLE_CONSTANTLY_CYCLE = Integer.parseInt(task_cycle_constantly_cycle);
		else
			TASK_CYCLE_CONSTANTLY_CYCLE = 20;

		if (StringUtils.isNotBlank(allStore))
			ALL_STORE = allStore.split(",");
		String task_cycle_everyday_start_date = (String) pro.getContextProperty("task_cycle_everyday_start_date");
		String task_cycle_constantly_start_date = (String) pro.getContextProperty("task_cycle_constantly_start_date");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			TASK_CYCLE_EVERYDAY_START_DATE = sdf.parse(task_cycle_everyday_start_date);
			TASK_CYCLE_CONSTANTLY_START_DATE = sdf2.parse(task_cycle_constantly_start_date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		if (StringUtils.isBlank(BROKER_URL) || StringUtils.isBlank(CURRENT_STORE)
				|| TASK_CYCLE_EVERYDAY_START_DATE == null
				|| TASK_CYCLE_CONSTANTLY_START_DATE == null) {
			try {
				throw new SyncDataException("参数不能为空");
			} catch (SyncDataException e) {
				e.printStackTrace();
			}
		}

		ZONGBU_FILE_SYNC_QUEUE = (String) pro.getContextProperty("zongbu_file_sync_queue");
		String all_store_file_sync_queue = (String) pro.getContextProperty("all_store_file_sync_queue");
		CURRENT_FILE_QUEUE = (String) pro.getContextProperty("current_file_queue");
		EXECUTE_SYNC_TASK = Boolean.valueOf((String) pro.getContextProperty("execute_sync_task"));
		if (StringUtils.isNotBlank(all_store_file_sync_queue))
			ALL_STORE_FILE_SYNC_QUEUE = all_store_file_sync_queue.split(",");
	}

	/**
	 * 
	 * @Desc 获取previous到now，每隔cycle分钟的时间段
	 * @author QUINN
	 * @date 2014年9月17日下午6:55:30
	 * @param now
	 * @param previous
	 * @param cycle
	 *            周期时间差
	 * @param calendarType
	 *            Calendar的几个时间常量
	 * @return
	 */
	public static List<Date[]> getTaskIntervalDate(Date now, Date previous, int calendarType, int cycle) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		List<Date[]> dates = new ArrayList<Date[]>();
		while (true) {
			Date currentDate = c.getTime();
			c.add(calendarType, -cycle);
			if (c.getTime().getTime() <= previous.getTime()) {
				dates.add(new Date[] { previous, currentDate });
				break;
			} else {
				dates.add(new Date[] { c.getTime(), currentDate });
			}
		}
		Collections.reverse(dates);
		return dates;
	}

	/**
	 * 
	 * @Desc 获取previous到now，每隔cycle分钟的时间段
	 * @author QUINN
	 * @date 2014年9月17日下午6:55:30
	 * @param now
	 * @param previous
	 * @param cycle
	 *            周期时间差
	 * @param calendarType
	 *            Calendar的几个时间常量
	 * @return
	 */
	public static List<Long[]> getTaskIntervalDate(long now, long previous, int calendarType, int cycle) {
		Date pre = new Date(previous);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(now));
		List<Long[]> dates = new ArrayList<Long[]>();
		while (true) {
			Date currentDate = c.getTime();
			c.add(calendarType, -cycle);
			if (c.getTime().getTime() <= pre.getTime()) {
				dates.add(new Long[] { pre.getTime(), currentDate.getTime() });
				break;
			} else {
				dates.add(new Long[] { c.getTime().getTime(), currentDate.getTime() });
			}
		}
		Collections.reverse(dates);
		return dates;
	}

}
